package adv8;

import java.io.File;
import java.io.FileWriter;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XmlDlete {

	public static void main(String[] args) throws Exception {

		// 修改student子节点
		SAXReader reader = new SAXReader();
		Document doc = reader.read(new File("students2.xml"));
		List<Element> list = doc.selectNodes("/stduents/student[@kgcno=\"0001\"]");
		if (list.size() == 0) {
			System.out.println("没有找啊到");
			return;
		}
		// list.get(0)
		for (Element e : list) {
			// 找到上级，再删除
			e.getParent().remove(e);
		}
		// 写回Xml，先设置格式，防止在一行不可读
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("utf-8");
		XMLWriter xm = new XMLWriter(new FileWriter("students2.xml"), format);
		xm.write(doc);
		xm.close();// 关闭
	}

}
